﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class _return : System.Web.UI.Page
    {
        SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
        string id, cd, idate, state;
        int fin;
        DateTime exp;
        double dy;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"];
            cnn.Open();
            SqlCommand cmd = new SqlCommand("select * from cdissue where issno='" + id + "'", cnn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                cd = Convert.ToString(dr.GetValue(1));
                exp = Convert.ToDateTime(dr.GetValue(4));
            }
            DateTime tdy = DateTime.Today;
            TimeSpan nody = tdy.Subtract(exp);
            dy = nody.TotalDays;
            if(dy.Equals(0))
            {
                SqlCommand cmd1=new SqlCommand("delete from cdissue where issno='"+id+"'",cnn);
                cmd1.ExecuteNonQuery();
                Response.Write("<script>alert('CD IS RETURNED')</script>");
                Response.Redirect("cdisuue.aspx");
            }
            else
            {
                fin = (int)dy * 5;
                SqlCommand cmd2 = new SqlCommand("update cdissue set fine='"+fin+"' where issno='" + id + "'", cnn);
                cmd2.ExecuteNonQuery();
                Response.Write("<script>alert('CD IS RETURNED')</script>");
            }
            cnn.Close();
        }
    }
}